package com.eshop.modules.activity.service.impl;

import java.lang.invoke.SerializedLambda;
import com.eshop.modules.product.domain.StoreProduct;
import com.eshop.enums.ProductTypeEnum;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.activity.service.dto.StoreVisitDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreVisitQueryCriteria;
import com.eshop.modules.product.service.StoreProductService;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.activity.service.StoreVisitService;
import com.eshop.modules.activity.domain.StoreVisit;
import com.eshop.modules.activity.service.mapper.StoreVisitMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreVisitServiceImpl extends BaseServiceImpl<StoreVisitMapper, StoreVisit> implements StoreVisitService
{
    private final IGenerator generator;
    private final StoreProductService storeProductService;
    private final StoreVisitMapper storeVisitMapper;
    
    public Map<String, Object> queryAll(final StoreVisitQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreVisit> page = (PageInfo<StoreVisit>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)StoreVisitDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreVisit> queryAll(final StoreVisitQueryCriteria criteria) {
        return (List<StoreVisit>)((StoreVisitMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreVisit.class, criteria));
    }
    
    public void download(final List<StoreVisitDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreVisitDto yxStoreVisit : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("产品ID", yxStoreVisit.getProductId());
            map.put("产品类型", yxStoreVisit.getProductType());
            map.put("产品分类ID", yxStoreVisit.getCateId());
            map.put("产品类型", yxStoreVisit.getType());
            map.put("用户ID", yxStoreVisit.getUid());
            map.put("访问次数", yxStoreVisit.getCount());
            map.put("备注描述", yxStoreVisit.getContent());
            map.put("添加时间", yxStoreVisit.getAddTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public void addStoreVisit(final Long uid, final Long productId) {
        final LambdaQueryWrapper<StoreVisit> wrapper = (LambdaQueryWrapper<StoreVisit>)new LambdaQueryWrapper();
        (wrapper.eq(StoreVisit::getUid, uid)).eq(StoreVisit::getProductId, productId);
        final StoreVisit storeVisit = (StoreVisit)((StoreVisitMapper)this.baseMapper).selectOne((Wrapper)wrapper);
        if (ObjectUtil.isNull(storeVisit)) {
            final StoreProduct storeProduct = this.storeProductService.getProductInfo(productId);
            final StoreVisit yxStoreVisit = StoreVisit.builder().productId(productId).productType(ProductTypeEnum.COMBINATION.getValue()).cateId(Integer.valueOf(storeProduct.getCateId())).type(ProductTypeEnum.COMBINATION.getValue()).uid(uid).count(1).build();
            this.save(yxStoreVisit);
        }
    }
    
    public StoreVisitServiceImpl(final IGenerator generator, final StoreProductService storeProductService, final StoreVisitMapper storeVisitMapper) {
        this.generator = generator;
        this.storeProductService = storeProductService;
        this.storeVisitMapper = storeVisitMapper;
    }
}
